package com.group18.controller;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.group18.entity.AccountUser;
import com.group18.entity.MemberUser;
import com.group18.service.AccountUserService;
import com.group18.service.MemberUserService;

/**
 * Servlet implementation class ToIndex
 */
@WebServlet("/ToIndex")
public class ToIndex extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ToIndex() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		
		HttpSession session = request.getSession();
		
		//查询用户所在家庭所有收支
		int family_id = (int)session.getAttribute("family_id");
		AccountUser au = new AccountUser();
		au.setFamily_id(family_id);
		AccountUserService aus = new AccountUserService();
		List<AccountUser> list = aus.selectAllAccountByFamily_id(au);
		
		//计算本月总收入、总支出；	给AccountUser中的classStr赋值;		给AccountUser中的background赋值
		Date d = new Date();
		SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
		String month = sdf1.format(d);
		double income = 0;//收入
		double spending = 0;//支出
		double remaining = 0;//余额
		for (AccountUser accountUser : list) {
			if(accountUser.getState().equals("收入")) {
				String date[] = accountUser.getDate().split("-");
				if(date[1].equals(month)) {
					income += accountUser.getMoney();
				}
				accountUser.setClassStr(aus.classStr(accountUser.getLabel()));
				accountUser.setBackground(aus.background(accountUser.getLabel()));
			}else {
				String date[] = accountUser.getDate().split("-");
				if(date[1].equals(month)) {
					spending += accountUser.getMoney();
				}
				accountUser.setClassStr(aus.classStr(accountUser.getLabel()));
				accountUser.setBackground(aus.background(accountUser.getLabel()));
			}
		}
		remaining = income - spending;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
        String date = sdf.format(d); 
        
        MemberUser mu = new MemberUser();
        int member_id = (int)session.getAttribute("member_id");
        MemberUserService mus = new MemberUserService();
        List<MemberUser> updatelist = mus.selectMemberByID(member_id);
        for (MemberUser memberUser : updatelist) {
        	session.setAttribute("name", memberUser.getName());
			session.setAttribute("member_id", memberUser.getId());
			session.setAttribute("username", memberUser.getUsername());
			session.setAttribute("family_id", memberUser.getFamily_id());
			session.setAttribute("password", memberUser.getPassword());
		}
        
        session.setAttribute("income", income);
        session.setAttribute("spending", spending);
        session.setAttribute("remaining", remaining);
        session.removeAttribute("text");
        
        String msg = (String)request.getAttribute("msg");
		request.setAttribute("msg", msg);
		request.setAttribute("list", list);
		request.setAttribute("date", date);
		request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
